//
//  XLMessagePresenter.m
//  industry
//
//  Created by xl on 2020/11/12.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLMessagePresenter.h"
#import "XLMessagePresenterProtocol.h"
#import "XLMessageViewProtocol.h"
#import "XLMessageModel.h"
#import "XLMessageInteractorProtocol.h"

@interface XLMessagePresenter ()<XLMessagePresenterProtocol>

@end

@implementation XLMessagePresenter

- (void)dealloc {
    XLLog(@"XLMessagePresenter dealloc");
}

#pragma mark - XLMessagePresenterProtocol
- (void)requestMessageList:(NSInteger)type page:(NSInteger)page searchStr:(NSString *)str {
    NSString *url = (type == 0) ? XLMessageUnReadListUrl:XLMessageReadListUrl;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@(page) forKey:@"pageNum"];
    [params setValue:@(XLPageSize) forKey:@"pageSize"];
    [params setValue:str forKey:@"param"];
    
    XLHttpRequest *request = [[XLHttpRequest alloc] init];
    WEAKSELF
    [request requestWithMethod:XLHttpMedthTypeGET url:url params:params isExtraParams:NO successBlock:^(NSDictionary *dict) {
        STRONGSELF
        id data = [dict objectForKey:@"data"];
        if ([data isKindOfClass:[NSDictionary class]]) {
            id temData = [(NSDictionary *)data objectForKey:@"records"];
            if ([temData isKindOfClass:[NSArray class]]) {
                NSArray *temArr = [XLMessageModel mj_objectArrayWithKeyValuesArray:temData];
                XLSend(strongSelf.baseController.cView, XLMessageViewProtocol, refreshTableView:temArr noData:(temArr.count < XLPageSize) ? YES:NO);
            } else {
                XLSend(strongSelf.baseController.cView, XLMessageViewProtocol, requestServerError:nil networkReachabilityStatus:XLNetworkReachabilityStatusUnkonw);
            }
        } else {
            XLSend(strongSelf.baseController.cView, XLMessageViewProtocol, requestServerError:nil networkReachabilityStatus:XLNetworkReachabilityStatusUnkonw);
        }
        
    } failBlock:^(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, NSError *error, NSDictionary *dict) {
        STRONGSELF
        XLSend(strongSelf.baseController.cView, XLMessageViewProtocol, requestServerError:error networkReachabilityStatus:reachability);
    }];
}

- (void)requestAlreadyReadMessages:(NSArray *)arr {
    NSMutableArray *temArr = [NSMutableArray array];
    for (XLMessageModel *model in arr) {
        if (!ISEmptyNumber(model.messageId)) {
            [temArr addObject:model.messageId.stringValue];
        }
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (temArr.count > 0) {
        NSString *msgIds = [temArr componentsJoinedByString:@","];
        [params setValue:msgIds forKey:@"msgIds"];
    }
    XLHttpRequest *request = [[XLHttpRequest alloc] init];
    XLSend(self.baseController.cView, XLMessageViewProtocol, showLoadingAnimalWithTitle:@"加载中...");
    WEAKSELF
    [request requestWithMethod:XLHttpMedthTypePOST url:XLOperationReadMessageUrl params:params isExtraParams:NO successBlock:^(NSDictionary *dict) {
        STRONGSELF
        XLSend(strongSelf.baseController.cView, XLMessageViewProtocol, hideLoadingAnimal);
        XLSend(strongSelf.baseController.cView, XLMessageViewProtocol, refreshTableViewData);
    } failBlock:^(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, NSError *error, NSDictionary *dict) {
        STRONGSELF
        XLSend(strongSelf.baseController.cView, XLMessageViewProtocol, hideLoadingAnimal);
    }];
}


- (void)requestDeleteMessages:(NSArray *)arr {
    NSMutableArray *temArr = [NSMutableArray array];
    for (XLMessageModel *model in arr) {
        if (!ISEmptyNumber(model.messageId)) {
            [temArr addObject:model.messageId.stringValue];
        }
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (temArr.count > 0) {
        NSString *msgIds = [temArr componentsJoinedByString:@","];
        [params setValue:msgIds forKey:@"msgIds"];
    }
    XLHttpRequest *request = [[XLHttpRequest alloc] init];
    XLSend(self.baseController.cView, XLMessageViewProtocol, showLoadingAnimalWithTitle:@"删除中...");
    WEAKSELF
    [request requestWithMethod:XLHttpMedthTypePOST url:XLDeleteMessagesUrl params:params isExtraParams:NO successBlock:^(NSDictionary *dict) {
        STRONGSELF
        XLSend(strongSelf.baseController.cView, XLMessageViewProtocol, hideLoadingAnimal);
        [XLToast showSuccessMessage:@"删除成功"];
        XLSend(strongSelf.baseController.cView, XLMessageViewProtocol, refreshTableViewData);
    } failBlock:^(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, NSError *error, NSDictionary *dict) {
        STRONGSELF
        XLSend(strongSelf.baseController.cView, XLMessageViewProtocol, hideLoadingAnimal);
    }];
}
- (void)requestDeleteMessage:(id)CellData {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    XLMessageModel *model = (XLMessageModel *)CellData;
    if (!ISEmptyNumber(model.messageId)) {
        [params setValue:model.messageId forKey:@"msgId"];
    }
    XLHttpRequest *request = [[XLHttpRequest alloc] init];
    XLSend(self.baseController.cView, XLMessageViewProtocol, showLoadingAnimalWithTitle:@"删除中...");
    WEAKSELF
    [request requestWithMethod:XLHttpMedthTypePOST url:XLDeleteMessageUrl params:params isExtraParams:NO successBlock:^(NSDictionary *dict) {
        STRONGSELF
        XLSend(strongSelf.baseController.cView, XLMessageViewProtocol, hideLoadingAnimal);
        [XLToast showSuccessMessage:@"删除成功"];
        XLSend(strongSelf.baseController.cView, XLMessageViewProtocol, refreshTableViewData);
    } failBlock:^(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, NSError *error, NSDictionary *dict) {
        STRONGSELF
        XLSend(strongSelf.baseController.cView, XLMessageViewProtocol, hideLoadingAnimal);
    }];
}

- (void)handleCellData:(id)cellData isRead:(BOOL)isRead {
    XLSend(self.baseController.interactor, XLMessageInteractorProtocol, gotoMessageDTVC:cellData isRead:isRead);
}
@end
